package com.gi.profiler;//Created by DimasTheDriver on May/26/2011. Part of the 'Android: changing the ringer's volume' post. Available at: http://www.41post.com/?p=3995

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.gi.profiler.listeners.VolumeSettingsListener;

public class VolumeActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.volume_settings);

	VolumeSettingsListener volListener = new VolumeSettingsListener(this);
	AudioManager audioMgr = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

	initSeekBar(((SeekBar) findViewById(R.id.sb_vol_media)),
		audioMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
		audioMgr.getStreamVolume(AudioManager.STREAM_MUSIC), volListener);

	initSeekBar((SeekBar) findViewById(R.id.sb_vol_alarm),
		audioMgr.getStreamMaxVolume(AudioManager.STREAM_ALARM),
		audioMgr.getStreamVolume(AudioManager.STREAM_ALARM), volListener);

	initSeekBar((SeekBar) findViewById(R.id.sb_vol_notif),
		audioMgr.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),
		audioMgr.getStreamVolume(AudioManager.STREAM_NOTIFICATION), volListener);

	initSeekBar((SeekBar) findViewById(R.id.sb_vol_ring),
		audioMgr.getStreamMaxVolume(AudioManager.STREAM_RING),
		audioMgr.getStreamVolume(AudioManager.STREAM_RING), volListener);

	initSeekBar((SeekBar) findViewById(R.id.sb_vol_sys),
		audioMgr.getStreamMaxVolume(AudioManager.STREAM_SYSTEM),
		audioMgr.getStreamVolume(AudioManager.STREAM_SYSTEM), volListener);

    }

    private void initSeekBar(SeekBar sb, int maxValidNum, int currentVal,
	    OnSeekBarChangeListener listener) {
	sb.setMax(maxValidNum);
	sb.setKeyProgressIncrement(1);
	sb.setProgress(currentVal);
	sb.setOnSeekBarChangeListener(listener);
    }

    public void onCancel(View v) {
	finish(); // Stop and end the current activity , remove it from the back
		  // stack then display the previous activity in the back stack
    }

    public void onSave(View v) {
	Intent intent = getIntent();
//	intent.putExtra("time", ((EditText) findViewById(R.id.txt_add_time)).getText().toString());
//	intent.putExtra("notes", ((EditText) findViewById(R.id.txt_add_note)).getText().toString());
	this.setResult(RESULT_OK, intent);
	finish();
    }
    // @Override
    // public boolean onKeyDown(int keyCode, KeyEvent event) {
    // // if one of the volume keys were pressed
    // if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode ==
    // KeyEvent.KEYCODE_VOLUME_UP) {
    // // change the seek bar progress indicator position
    // volumeBar.setProgress(audioMgr.getStreamVolume(AudioManager.STREAM_RING));
    // }
    // // propagate the key event
    // return super.onKeyDown(keyCode, event);
    // }
}